//
//  AY_BookCell0.m
//  AiYue
//
//  Created by boWen on 2023/10/18.
//

#import "AY_BookCell0.h"
#import "SDCycleScrollView.h"
#import "AY_MainTabViewController.h"
#import "AY_BookVpPage.h"
#import "AY_BookDPage.h"
#import "AY_BookQianDPage.h"

@interface AY_BookCell0 ()<SDCycleScrollViewDelegate>
@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic, strong) NSMutableArray *bannerArray;
@end


@implementation AY_BookCell0

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = COLOR_Bg;
        self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(15, 0, SCREEN_W - 30, 130) delegate:self placeholderImage:nil];
        self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        self.cycleScrollView.layer.cornerRadius = YNUM(8);
        self.cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        self.cycleScrollView.layer.masksToBounds = YES;
        self.cycleScrollView.backgroundColor = COLOR_Bg;
        self.cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleToFill;
        self.cycleScrollView.autoScrollTimeInterval = 5;
//        self.cycleScrollView.currentPageDotColor = THEME_COLOR;
//        self.cycleScrollView.pageDotColor = [UIColor whiteColor];
        [self addSubview:self.cycleScrollView];
        
//        CALayer *subLayer=[CALayer layer];
//        CGRect fixframe = self.cycleScrollView.frame;
//        subLayer.frame= fixframe;
//        subLayer.cornerRadius=YNUM(8);
//        subLayer.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.3].CGColor;
//        subLayer.masksToBounds=NO;
//        subLayer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
//        subLayer.shadowOffset = CGSizeMake(3,2);//shadowOffset阴影偏移,x向右偏移3，y向下偏移2，默认(0, -3),这个跟shadowRadius配合使用
//        subLayer.shadowOpacity = 0.8;//阴影透明度，默认0
//        subLayer.shadowRadius = 5;//阴影半径，默认3
//        [self.layer insertSublayer:subLayer below:self.cycleScrollView.layer];
    }
    return self;
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    BookDecModel *model = self.model.items[index];
    if ([model.redirectType isEqual:@"1"]) {
        AY_PageReadViewController *vc = [[AY_PageReadViewController alloc] init];
        [vc sendBookId:model.redirectTypeName andPage:@"0" andChapterIndex:@"0"];
        [[AYTool viewControllerWith:self].navigationController pushViewController:vc animated:YES];
    }else if ([model.redirectType isEqual:@"2"]) {
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        UINavigationController *nav = (UINavigationController *) window.rootViewController;
        AY_MainTabViewController *rootViewController = nav.viewControllers[0];
        [rootViewController setSelectedIndex:2];
    }else if ([model.redirectType isEqual:@"3"]) {
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        UINavigationController *nav = (UINavigationController *) window.rootViewController;
        AY_MainTabViewController *rootViewController = nav.viewControllers[0];
        [rootViewController setSelectedIndex:3];
    }else if ([model.redirectType isEqual:@"4"]) {
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        UINavigationController *nav = (UINavigationController *) window.rootViewController;
        AY_MainTabViewController *rootViewController = nav.viewControllers[0];
        [rootViewController setSelectedIndex:3];
    }else if ([model.redirectType isEqual:@"5"]) {
        AY_BookVpPage * page = [[AY_BookVpPage alloc] init];
        [[AYTool viewControllerWith:self].navigationController pushViewController:page animated:YES];
    }else if ([model.redirectType isEqual:@"6"]) {
        AY_BookDPage * vc = [[AY_BookDPage alloc] init];
        [[AYTool viewControllerWith:self].navigationController pushViewController:vc animated:YES];
    }else if ([model.redirectType isEqual:@"7"]) {
        AY_BookQianDPage * vc = [[AY_BookQianDPage alloc] init];
        [[AYTool viewControllerWith:self].navigationController pushViewController:vc animated:YES];
    }else if ([model.redirectType isEqual:@"8"]) {
        AY_WebViewController *webVC = [[AY_WebViewController alloc] init];
        webVC.fullPath = [NSString stringWithFormat:@"%@",model.redirectTypeName];
        [[AYTool viewControllerWith:self].navigationController pushViewController:webVC animated:YES];
    }
}

- (void)setModel:(ChannelListModel *)model {
    _model = model;
    self.bannerArray = [NSMutableArray array];
    if(!kStringIsEmpty(_model.controlType) && [_model.controlType isEqualToString:@"ad"]) {
        for (BookDecModel *decModel in model.items) {
            [self.bannerArray addObject:decModel.image];
        }
    }
    self.cycleScrollView.imageURLStringsGroup = self.bannerArray;
}

@end
